var context_path = "/jlitetest";
// instance
var elite = new Lite();
elite.litejs_context_path = context_path.concat("/js/LiteJS/");
elite.use(["http", "core", "move", "UI"], _nextStep);

function _nextStep() {
    elite.appendSource(["/jlitetest/js/mui/mui.min.js", "/jlitetest/css/mui.css", "/jlitetest/css/common.css"], _callback);
}

function createIframe(el, opt) {
    var elContainer = document.querySelector(el);
    var wrapper = document.querySelector("._iframe-wrapper");
    if (!wrapper) {
        // 创建wrapper 和 _iframe
        wrapper = document.createElement('div');
        wrapper.className = '_iframe-wrapper';
        var _iframe = document.createElement('iframe');
        _iframe.src = opt.url;
        _iframe.id = opt.id || opt.url;
        _iframe.name = opt.id;
        wrapper.appendChild(_iframe);
        elContainer.appendChild(wrapper);
    } else {
        var _iframe = wrapper.querySelector('iframe');
        _iframe.src = opt.url;
        _iframe.id = opt.id || opt.url;
        _iframe.name = _iframe.id;
    }
    // 重新设置样式
    for (var key in opt.style) {
        wrapper.key = opt.style.key;
    }
}

function getQueryVariable(variable){
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] === variable){return pair[1];}
    }
    return(false);
}

function getLocalTime(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}

function serializeNode(formNode){
    var res = [],	//存放结果的数组
        current = null,	//当前循环内的表单控件
        i,	//表单NodeList的索引
        len, //表单NodeList的长度
        k,	//select遍历索引
        optionLen,	//select遍历索引
        option, //select循环体内option
        optionValue,	//select的value
        form = formNode;	//用form变量拿到当前的表单，易于辨识
    for(i=0, len=form.elements.length; i<len; i++){
        current = form.elements[i];
        //disabled表示字段禁用，需要区分与readonly的区别
        if(current.disabled) continue;
        switch(current.type){
            //可忽略控件处理
            case "file":	//文件输入类型
            case "submit":	//提交按钮
            case "button":	//一般按钮
            case "image":	//图像形式的提交按钮
            case "reset":	//重置按钮
            case undefined:	//未定义
                break;
            //select控件
            case "select-one":
            case "select-multiple":
                if(current.name && current.name.length){
                    console.log(current);
                    for(k=0, optionLen=current.options.length; k<optionLen; k++){

                        option = current.options[k];
                        optionValue = "";
                        if(option.selected){
                            if(option.hasAttribute){
                                optionValue = option.hasAttribute('value') ? option.value : option.text
                            }else{
                                //低版本IE需要使用特性 的specified属性，检测是否已规定某个属性
                                optionValue = option.attributes('value').specified ? option.value : option.text;
                            }
                            res.push(encodeURIComponent(current.name) + "=" + encodeURIComponent(optionValue));
                        }
                    }
                }
                break;
            //单选，复选框
            case "radio":
            case "checkbox":
                //这里有个取巧 的写法，这里的判断是跟下面的default相互对应。
                //如果放在其他地方，则需要额外的判断取值
                if(!current.checked) break;
            default:
                //一般表单控件处理
                if(current.name && current.name.length){
                    res.push(encodeURIComponent(current.name) + "=" + encodeURIComponent(current.value));
                }
        }
    }
    return res.join("&");
}

function json2params(jsonObj){
    return Object.keys(jsonObj).map(function (key) {
        // body...
        return encodeURIComponent(key) + "=" + encodeURIComponent(jsonObj[key]);
    }).join("&");
}
